package com.wei.web.demo.user.config;

import com.wei.web.demo.user.filters.CustomClassFilter;
import com.wei.web.demo.user.filters.CustomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

@Configuration
@ConditionalOnProperty(value = "fas.enable", havingValue = "no", matchIfMissing = true)
public class FilterConfiguration {
    @Autowired
    private CustomFilter customFilter;

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> addCustomClassFilter() {
        FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CustomClassFilter());
        registration.addUrlPatterns("/*");
        registration.setName("customClassFilter");
        registration.setOrder(0);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<OncePerRequestFilter> addCustomFilter() {
        FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(customFilter);
        registration.addUrlPatterns("/*");
        registration.setName("customFilter");
        registration.setOrder(1);
        return registration;
    }
}
